package com.ihd.ihardwarefactorytools.common.utils;

import cn.wowjoy.commonlibrary.utils.HexUtils;

public class DataUtils {
    private static String getName(byte[] data) {
        return String.valueOf((char) data[5]) + String.valueOf((char) data[6]) + String.valueOf((char) data[7]) + String.valueOf((char) data[8]);
    }

    private static String getOKOKName(byte[] data) {
        return String.valueOf((char) data[5]) + String.valueOf((char) data[6]) + String.valueOf((char) data[7]);
    }

    public static boolean isValidDevice(byte[] data) {
        return data.length == 31 && ("VCHM".equals(getName(data)) || "ADV".equals(getOKOKName(data)));
    }

    public static boolean isStable(byte[] data) {
        String tag;
        if ("VCHM".equals(getName(data)))
            tag = HexUtils.byteToBinaryString(data[17]);
        else
            tag = HexUtils.byteToBinaryString(data[18]);
        if ("1".equals(tag.substring(7, 8)))
            return true;
        else
            return false;
    }

    public static String getWeight(byte[] data) {
        byte[] weight;
        if ("VCHM".equals(getName(data)))
            weight = new byte[]{data[19], data[20]};
        else
            weight = new byte[]{data[20], data[21]};
        return HexUtils.byteArrayToInt(weight) / 100 + "." + HexUtils.byteArrayToInt(weight) % 100;
    }

    public static String getOmu(byte[] data) {
        byte[] weight;
        if ("VCHM".equals(getName(data)))
            weight = new byte[]{data[21], data[22]};
        else
            weight = new byte[]{data[22], data[23]};
        return HexUtils.byteArrayToInt(weight)+"";
    }

    public static String getMacAddress(byte[] data) {
        byte[] mac;
        if ("VCHM".equals(getName(data)))
            mac = new byte[]{data[23], data[24], data[25], data[26], data[27], data[28]};
        else
            mac = new byte[]{data[24], data[25], data[26], data[27], data[28], data[29]};
        return HexUtils.bytesToHex(mac);
    }
}
